import { PlayerBase } from './PlayerBase.js'

/**
 * AI玩家类，继承自PlayerBase
 * 实现AI控制逻辑
 */
export class AIPlayer extends PlayerBase {
    constructor(gameMap, info) {
        super(gameMap, info)
        // 初始化AI难度，默认为normal
        this.difficulty = this.gameMap.difficulty || 'normal'
    }

    start() {
        // AI不需要额外初始化
    }

    // 更新AI玩家移动
    update_player_move() {
        if (this.damageSpeed > 10) {
            this.vx = this.vy = 0
            this.moveLength = 0
            this.info.x += (this.damageVx * this.damageSpeed * this.timeDelta) / 1000
            this.info.y += (this.damageVy * this.damageSpeed * this.timeDelta) / 1000
            this.damageSpeed *= this.friction
        } else {
            if (this.moveLength < this.eps) {
                this.vx = this.vy = 0
                this.moveLength = 0
                // AI会随机移动
                let tx = Math.random() * this.width
                let ty = Math.random() * this.height
                this.move_to(tx, ty)
            } else {
                let moved = Math.min(this.moveLength, (this.info.speed * this.timeDelta) / 1000)
                this.info.x += this.vx * moved
                this.info.y += this.vy * moved
                this.moveLength -= moved
            }
        }
    }

    // 根据难度更新AI玩家的行为
    update_AI_behavior() {
        // 简单模式：AI只会随机移动，不会攻击
        if (this.difficulty === 'easy') {
            // 简单模式下不进行攻击
        }
        // 普通模式：AI会随机选择目标攻击，4秒保护时间
        else if (this.difficulty === 'normal' && this.spentTime > 4 && Math.random() < 1 / 300.0) {
            let player =
                this.gameMap.players[Math.floor(Math.random() * this.gameMap.players.length)]
            let tx = player.info.x + ((player.vx * player.info.speed * this.timeDelta) / 1000) * 0.3
            let ty = player.info.y + ((player.vy * player.info.speed * this.timeDelta) / 1000) * 0.3
            this.skills.fireball.cast(tx, ty)
        }
        // 困难模式：AI会瞄准玩家，4秒保护时间
        else if (this.difficulty === 'hard' && this.spentTime > 4 && Math.random() < 1 / 150.0) {
            // 找到玩家角色
            let player = null
            for (let i = 0; i < this.gameMap.players.length; i++) {
                if (this.gameMap.players[i].info.role === 'me') {
                    player = this.gameMap.players[i]
                    break
                }
            }

            if (player) {
                let tx =
                    player.info.x + ((player.vx * player.info.speed * this.timeDelta) / 1000) * 0.5
                let ty =
                    player.info.y + ((player.vy * player.info.speed * this.timeDelta) / 1000) * 0.5
                this.skills.fireball.cast(tx, ty)
            }
        }
        // 专家模式：AI会瞄准玩家，无保护时间，攻击更频繁
        else if (this.difficulty === 'expert' && Math.random() < 1 / 100.0) {
            // 找到玩家角色
            let player = null
            for (let i = 0; i < this.gameMap.players.length; i++) {
                if (this.gameMap.players[i].info.role === 'me') {
                    player = this.gameMap.players[i]
                    break
                }
            }

            if (player) {
                let tx =
                    player.info.x + ((player.vx * player.info.speed * this.timeDelta) / 1000) * 0.7
                let ty =
                    player.info.y + ((player.vy * player.info.speed * this.timeDelta) / 1000) * 0.7
                this.skills.fireball.cast(tx, ty)
            }
        }
    }

    update() {
        if (this.isDead) {
            return
        }

        // 增加保护时间计数
        this.spentTime += this.timeDelta / 1000

        this.update_player_move()

        // 仅在PvE模式下执行AI行为
        if (this.gameMap.constructor.name === 'PvEGameMap') {
            this.update_AI_behavior()
        }

        this.draw()
    }

    // AI玩家不显示技能图标
    draw_skill_icon() {
        // AI不显示技能图标
    }
}
